// 自定义导航快捷功能
function setIndexModule() {
	var mTitle = '自定义导航快捷功能',
		mUrl = '/system/menu/menu_select',
		mWidth = '670',
		mHeight = '500';
	$.modal.open(mTitle, mUrl, mWidth, mHeight, function (index) {
		var res = window["layui-layer-iframe" + index].callbackdata();
		if (res) {
			var postData = {
				'infoItems': res
			};
			$.operate.post('/system/indexmodule/pspost', postData, function (res) {
				getIndexmodule();
			});
		}
		layer.close(index);
	})
}
//获取首页常用栏目
function getIndexmodule() {
	$.get('/system/indexmodule/list', function (res) {
		var str = '';
		if (res.count > 0) {
			str += '<ul class="layui-row layui-col-space10" style="display: contents">';
			$.each(res.data, function (i, v) {
				if (v.moduleICON) {
					str += '<li class="layui-col-xs2"><a ><i class="' + v.moduleICON + '"></i>';
				} else str += '<li class="layui-col-xs2"><a ><i class="layui-icon layui-icon-set"></i>';
				if (v.moduleurl1 != '#' && v.moduleurl2 != '#') {
					str += '<a lay-href="' + v.moduleurl1 + '" lay-text="' + v.moduleName + ' - 查询" class="i-btn display_no">查询</a>' +
						'<a lay-href="' + v.moduleurl2 + '" lay-text="' + v.moduleName + ' - 新增" class="i-btn i-zd display_no">制单</a>';
				} else if (v.moduleurl1 && v.moduleurl2 == '#') {
					str += '<a lay-href="' + v.moduleurl1 + '" lay-text="' + v.moduleName + ' - 查询" class="i-btn display_no btn-w">查询</a>';
				} else if (v.moduleurl1 == '#' && v.moduleurl2) {
					str += '<a lay-href="' + v.moduleurl2 + '" lay-text="' + v.moduleName + ' - 新增" class="i-btn display_no btn-w">制单</a>';
				}
				str += '<cite>' + v.moduleName + '</cite></a></li>';
			})
			str += '</ul>';
			$("#indexModule").html(str);
			$("#indexModule ul li").hover(
				function () {
					$(this).find(".layui-icon").css("opacity", 0);
					$(this).find(".i-btn").removeClass("display_no");
				},
				function () {
					$(this).find(".layui-icon").css("opacity", 1);
					$(this).find(".i-btn").addClass("display_no");
				}
			);
		} else {
			str += '<ul class="layui-row layui-col-space10" style="display: contents"><li class="layui-col-xs3 addModule" onclick="setIndexModule()">' +
				'<a><i class="layui-icon layui-icon-add-circle"></i><cite>新增快捷功能</cite></a></li></ul>';
			$("#indexModule").html(str);
		}
	});
}
//获取首页信息
function getIndexMsg(url, msgType, objID) {
	$.get(url, function (res) {
		var str = '';
		if (res.count > 0) {
			if (msgType == 'msg') {
				$.each(res.data, function (i, v) {
					var queryConditions = v.queryConditions ? '?' + v.queryConditions : '';
					str += '<div class="layuimini-notice"><div class="layuimini-notice-title" title="' + v.vMessage + '">' + v.vMessage + '</div>' +
						'<div class="layuimini-notice-extra">' + v.createTime + '</div>' +
						'<div class="layuimini-notice-url layui-hide">' + v.pageUrl + queryConditions + '</div>' +
						'<div class="layuimini-notice-reMark layui-hide">' + v.reMark + '</div>' +
						'<div class="layuimini-notice-vtype layui-hide">' + v.vType + '</div>' +
						'<div class="layuimini-notice-moduleName layui-hide">' + v.moduleName + '</div></div>';
				})
			} else {
				$.each(res.data, function (i, v) {
					var queryConditions = v.queryConditions ? '?' + v.queryConditions : '';
					str += '<tr class="layuimini-notice" title="点击查看"><td style="width: 70%" class="layuimini-notice-title">' + v.vTitle + '</td><td align="center" style="color: red">' + v.fJe + '</td>' +
						'<td class="layuimini-notice-url layui-hide">' + v.pageUrl + queryConditions + '</td>' +
						'<td class="layuimini-notice-vtype layui-hide">' + v.vType + '</td>' +
						'<td class="layuimini-notice-moduleName layui-hide">' + v.moduleName + '</td></tr>';
				})
				$("#" + objID + " tbody").append(str);
				return;
			}

		} else {
			str += '<div class="layuimini-notice"><div class="layuimini-notice-title">暂无消息</div></div>';
		}
		$("#" + objID).html(str);
	})
}
//查看首页信息
$('body').on('click', '.layuimini-notice', function () {
	var title = $(this).children('.layuimini-notice-title').text(),
		noticeTime = $(this).children('.layuimini-notice-extra').text(),
		noticeVtype = $(this).children('.layuimini-notice-vtype').text(),
		noticeReMark = $(this).children('.layuimini-notice-reMark').text(),
		moduleName = $(this).children('.layuimini-notice-moduleName').text(),
		notice_url = $(this).children('.layuimini-notice-url').html();
	if (title == "暂无消息") return;
	if (noticeVtype == 'newpage') {
		parent.layui.index.openTabsPage(notice_url, moduleName);
	} else {
		var content, type;
		if (noticeVtype == 'no') {
			type = 1;
			$(".msg_title").html(title);
			var moreUrl = '';
			if (notice_url) moreUrl = '<a style="color: #1E9FFF;" href="javascript:parent.layui.index.openTabsPage(\'' + notice_url + '\',\'' + moduleName + '\');parent.layer.closeAll()">查看>></a>';
			$(".msg_content").html(noticeReMark + moreUrl);
			content = $(".msg_contet").html();
		} else {
			type = 2;
			content = notice_url;
		}
		var btnList = ['关闭'],
			area = ['30%', ($(window).height() - 350) + 'px'],
			btnFun = false;
		if (notice_url.indexOf('pageType=allot') > 0) {
			btnList = ['确定', '关闭'];
			area = ['50%', '500px'];
			btnFun = true;
		} else if (notice_url.indexOf('pageType=claim') > 0) {
			btnList = ['认领', '关闭'];
			area = ['50%', '500px'];
			btnFun = true;
		}
		parent.layer.open({
			type: type,
			title: moduleName + '<span style="float: right;right: 1px;font-size: 12px;color: #b1b3b9;margin-top: 1px">' + noticeTime + '</span>',
			offset: '10%',
			area: area,
			shade: 0.8,
			shadeClose: true,
			id: 'layuimini-notice',
			btn: btnList,
			yes: function (index, layero) {
				if (btnFun) {
					var res = parent.window["layui-layer-iframe" + index].callbackdata();
					var confirmStr = '';
					if (!res.sUser) {
						parent.layer.alert('请选择商务员', {
							icon: 2,
							offset: '10%'
						});
						return false;
					}
					if (res.vFpLx == '经理分配') {
						confirmStr = '确认分配 "' + res.sUserName + '" 为客户 "' + res.gCName + '“ 的商务员吗？'
					} else {
						confirmStr = '确定认领客户 "' + res.gCName + '" 吗？'
					}
					parent.layer.confirm(confirmStr, {
						icon: 3,
						title: '提示',
						offset: '10%'
					}, function (index) {
						var pd = [];
						pd.push(res);
						$.post('/crm/customersuserapply/updatesuserinfo', {
							'infoItems': JSON.stringify(pd)
						}, function (data) {
							if (data.code == 0) {
								parent.layer.alert(data.msg, {
									icon: 1,
									offset: '10%'
								});
								setTimeout(function () {
									parent.layer.closeAll()
								}, 3000);
								getIndexMsg('/system/indexmodule/messagelist', 'msg', 'notice_list');
							} else {
								parent.layer.alert(data.msg, {
									icon: 2,
									offset: '10%'
								});
							}
						}, 'json');
					});
				} else {
					parent.layer.closeAll()
				}
			},
			cancel: function (index, layero) {
				parent.layer.close(index)
				return false;
			},
			moveType: 1,
			content: content
		});
	}
});